package com.ifar.audiosdk.service

import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.Service
import android.content.Intent
import android.os.Build
import android.os.IBinder
import android.os.Vibrator
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat
import com.blankj.utilcode.util.AppUtils
import com.ifar.audiosdk.R

class NotificationGroupService : Service() {
    private lateinit var vibrator: Vibrator

    override fun onBind(intent: Intent): IBinder? {
        return null
    }

    override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int {
        //use constant ID for notification used as group summary
        val GROUP_KEY_WORK_EMAIL = "com.android.example.WORK_EMAIL"

        val notificationManager = NotificationManagerCompat.from(this)

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val channel = NotificationChannel(
                packageName,
                packageName + AppUtils.getAppName(),
                NotificationManager.IMPORTANCE_HIGH
            )
            notificationManager.createNotificationChannel(channel)
        }

        val newMessageNotification1 = NotificationCompat.Builder(this, packageName)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle("111")
            .setContentText("You will not believe...")
            .setGroup(GROUP_KEY_WORK_EMAIL)
            .build()

        val newMessageNotification2 = NotificationCompat.Builder(this, packageName)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle("222")
            .setContentText("Please join us to celebrate the...Please join us to celebrate the...Please join us to celebrate the...Please join us to celebrate the...Please join us to celebrate the...")
            .setGroup(GROUP_KEY_WORK_EMAIL)
            .build()

        val summaryNotification = NotificationCompat.Builder(this, packageName)
            .setContentTitle("333")
            .setContentText("Two new messages")
            .setSmallIcon(R.mipmap.ic_launcher)
            .setStyle(NotificationCompat.InboxStyle()
                .addLine("Alex Faarborg Check this out")
                .addLine("Jeff Chang Launch Party")
                .setBigContentTitle("2 new messages")
                .setSummaryText("janedoe@example.com"))
            .setGroup(GROUP_KEY_WORK_EMAIL)
            .setGroupSummary(true)
            .build()

        NotificationManagerCompat.from(this).apply {
            notify(1, newMessageNotification1)
            notify(2, newMessageNotification2)
            notify(3, summaryNotification)
        }

        return super.onStartCommand(intent, flags, startId)
    }

}